package state;

import particle.core.tools.PMath;
import state.component.Style;
import state.util.array.ObjectArray;
import state.util.array.StringArray;

public class AlertMenu extends GameForm {
  private ObjectArray iconImages = null;
  private StringArray menu = null;
  private AlertListener alertListener = null;
  private int selectedIndex = 0;
  private int alertId = 0;

  private Style style = new Style();

  private static AlertMenu singleton = null;

  private static AlertMenu instance() {
    if (singleton == null) {
      singleton = new AlertMenu();
    }
    return singleton;
  }

  private void initAlert(AlertListener alertListener1, ObjectArray iconImages1, StringArray menu1, int alertId1) {
    this.alertListener = alertListener1;
    this.iconImages = iconImages1;
    this.menu = menu1;
    this.alertId = alertId1;
    selectedIndex = 0;
    resize(10, menu1.len() * 30);
  }

  private void resize(int width1, int heigth1) {
    setSize(width1, heigth1);
    setPosition(GameConstants.SCREEN_WIDTH / 2 - width1 / 2, GameConstants.SCREEN_HEIGHT / 2 - heigth1 / 2);
  }

  public static void setStyle(Style style) {
    AlertMenu alertMenu = AlertMenu.instance();
    alertMenu.style = style;
  }

  public static void showAlert(AlertListener alertListener, ObjectArray iconImages, StringArray menu, int alertId) {
    AlertMenu alertMenu = AlertMenu.instance();
    alertMenu.initAlert(alertListener, iconImages, menu, alertId);
    GameGlobal.alert.showCustomUIAlert(alertMenu, alertId);
  }

  public void draw(Graphics g) {
    int maxWidth = 0;
    int maxHeight = 0;
    int maxImageWidth = 0;

    if (style.isFillBackGround()) {
      g.setColor(style.getBackgroundColor());
      g.fillRect(getRealX(), getRealY(), getWidth(), getHeight());
    }

    for (int i = 0; i < menu.len(); i++) {
      int rowHeight = 0;
      int rowWidth = 0;
      if ((iconImages == null || iconImages.len() <= i || iconImages.get(i) == null)) {
        rowHeight = 4 + style.getFont().getHeight();
        rowWidth = style.getFont().stringWidth(menu.get(i));
      } else {
        rowHeight = 4 + PMath.max(style.getFont().getHeight(), ((GameImage) iconImages.get(i)).getHeight());
        maxImageWidth = PMath.max(maxImageWidth, ((GameImage) iconImages.get(i)).getWidth());
        rowWidth = style.getFont().stringWidth(menu.get(i)) + maxImageWidth + 10;
      }
      maxHeight += rowHeight;
      if (rowWidth > maxWidth) {
        maxWidth = rowWidth;
      }

      int textColor = style.getForceGroundColor();
      if (i == selectedIndex) {
        g.setColor(style.getFocusBgColor());
        g.fillRect(getRealX(), getRealY() + maxHeight - rowHeight, maxWidth, rowHeight);

        textColor = style.getFocusFgColor();
      }

      if (iconImages != null && iconImages.len() > i && iconImages.get(i) != null) {
        g.drawImage((GameImage) iconImages.get(i), getRealX() + 2, getRealY() + maxHeight - rowHeight + 2, GameConstants.TOP_LEFT_ANCHOR);
      }
      style.getFont().drawString2(g, menu.get(i), textColor, getRealX() + maxImageWidth + 7, getRealY() + maxHeight - rowHeight + 1, GameConstants.TOP_LEFT_ANCHOR);
    }

    if (style.isHasBorder()) {
      g.setColor(style.getBorderColor());
      g.drawRect(getRealX(), getRealY(), getWidth() - 1, getHeight() - 1);
    }
    resize(maxWidth, maxHeight);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.UP:
      selectedIndex = (--selectedIndex < 0) ? 0 : selectedIndex;
      break;
    case Key.DOWN:
      selectedIndex = (++selectedIndex > menu.len() - 1) ? menu.len() - 1 : selectedIndex;
      break;
    case Key.FIRE:
      GameGlobal.systemCanvas.hideAlert();
      if (alertListener != null) {
        alertListener.alertEventPerform(GameAlert.CUSTOM_UI_TYPE, selectedIndex, alertId);
      }
      destroy();
      break;
    }
  }

  public void destroy() {
    iconImages = null;
    menu = null;
    alertListener = null;
  }
}
